package com.eye.catcher.service.generate.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eye.catcher.utils.RandomIdUtils;
import com.eye.catcher.constants.SbCode;
import com.eye.catcher.entity.TSysDaUserRole;
import com.eye.catcher.mapper.TSysDaUserRoleMapper;
import com.eye.catcher.service.generate.ITSysDaUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eye.catcher.system.userRole.param.UserRoleDto;
import com.eye.catcher.system.userRole.param.UserRolePage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huzhiwei
 * @since 2022-02-08
 */
@Slf4j
@Service
public class TSysDaUserRoleServiceImpl extends ServiceImpl<TSysDaUserRoleMapper, TSysDaUserRole> implements ITSysDaUserRoleService {

    @Override
    public void addUserRole(TSysDaUserRole userRole) {
        userRole.setId(RandomIdUtils.getGuid());
        userRole.setLastTime(LocalDateTime.now());
        userRole.setSource(SbCode.DATA_SOURCE_CREATE.getCode());
        this.save(userRole);
    }

    @Override
    public void addBatchUserRole(TSysDaUserRole userRole) {
        if(SbCode.DATA_DELETE_AFTER_ADD.getCode().equals(userRole.getType())){
            //先删除
            if(StringUtils.isNotEmpty(userRole.getUserIds())){
                QueryWrapper<TSysDaUserRole> wrapper = Wrappers.query();
                wrapper.in("USER_ID",Arrays.asList(userRole.getUserIds().split(",")));
                this.remove(wrapper);
            }
        }
        List<TSysDaUserRole> saveBatchList = new ArrayList<>();
        TSysDaUserRole userRole1 = null;
        for(String userId:userRole.getUserIds().split(",")){
            for(String roleId:userRole.getRoleIds().split(",")){
                userRole1 = new TSysDaUserRole();
                userRole1.setId(RandomIdUtils.getGuid());
                userRole1.setRoleId(roleId);
                userRole1.setUserId(userId);
                userRole1.setType(SbCode.DATA_NORMAL.getCode());
                userRole1.setVersion("1");
                userRole1.setLastTime(LocalDateTime.now());
                saveBatchList.add(userRole1);
            }
        }
        this.saveBatch(saveBatchList);
    }

    @Override
    public void addBatchRoleUser(TSysDaUserRole userRole) {
        if(SbCode.DATA_DELETE_AFTER_ADD.getCode().equals(userRole.getType())){
            //先删除
            if(StringUtils.isNotEmpty(userRole.getRoleIds())){
                QueryWrapper<TSysDaUserRole> wrapper = Wrappers.query();
                wrapper.in("ROLE_ID",Arrays.asList(userRole.getRoleIds().split(",")));
                this.remove(wrapper);
            }
        }
        List<TSysDaUserRole> saveBatchList = new ArrayList<>();
        TSysDaUserRole userRole1 = null;
        for(String roleId:userRole.getRoleIds().split(",")){
            for(String userId:userRole.getUserIds().split(",")){
                userRole1 = new TSysDaUserRole();
                userRole1.setId(RandomIdUtils.getGuid());
                userRole1.setRoleId(roleId);
                userRole1.setUserId(userId);
                userRole1.setType(SbCode.DATA_NORMAL.getCode());
                userRole1.setVersion("1");
                userRole1.setLastTime(LocalDateTime.now());
                saveBatchList.add(userRole1);
            }
        }
        this.saveBatch(saveBatchList);
    }

    @Override
    public Object getUserRoleList(UserRoleDto userRole) {
        QueryWrapper<TSysDaUserRole> wrapper = Wrappers.query();
        //组装查询条件
        if(StringUtils.isNotEmpty(userRole.getRoleId())){
            wrapper.eq("ROLE_ID",userRole.getRoleId());
        }
        if(StringUtils.isNotEmpty(userRole.getUserId())){
            wrapper.eq("USER_ID",userRole.getUserId());
        }

        return this.list(wrapper);
    }

    @Override
    public void deleteUserRoleByIds(String userRoleIds) {
        this.removeByIds(Arrays.asList(userRoleIds.split(",")));
    }

    @Override
    public void deleteUserRoleByUserIds(String userIds,String roleIds) {
        QueryWrapper<TSysDaUserRole> wrapper = Wrappers.query();
        if(StringUtils.isNotEmpty(userIds)) {
            wrapper.in("USER_ID", Arrays.asList(userIds.split(",")));
        }
        if(StringUtils.isNotEmpty(roleIds)){
            wrapper.in("ROLE_ID",Arrays.asList(roleIds.split(",")));
        }
        this.remove(wrapper);
    }

    @Override
    public void deleteUserRoleByRoleIds(String userIds,String roleIds) {
        QueryWrapper<TSysDaUserRole> wrapper = Wrappers.query();
        if(StringUtils.isNotEmpty(userIds)) {
            wrapper.in("USER_ID", Arrays.asList(userIds.split(",")));
        }
        if(StringUtils.isNotEmpty(roleIds)){
            wrapper.in("ROLE_ID",Arrays.asList(roleIds.split(",")));
        }
        this.remove(wrapper);
    }

    @Override
    public Object getPageUserRoleList(UserRolePage userRolePage) {
        Page<TSysDaUserRole> page=new Page<>(userRolePage.getPage().getPageNum(),userRolePage.getPage().getPageSize());
        QueryWrapper<TSysDaUserRole> wrapper = Wrappers.query();
        //组装查询条件
        if(StringUtils.isNotEmpty(userRolePage.getUserRole().getRoleId())){
            wrapper.eq("ROLE_ID",userRolePage.getUserRole().getRoleId());
        }
        if(StringUtils.isNotEmpty(userRolePage.getUserRole().getUserId())){
            wrapper.eq("USER_ID",userRolePage.getUserRole().getUserId());
        }

//        wrapper.eq("STATUS", SbCode.DATA_NORMAL.getCode());
        wrapper.orderByDesc(" LAST_TIME ");
        //list
        IPage<TSysDaUserRole> returnPage = this.page(page, wrapper);
        //总数
        returnPage.setTotal(this.count(wrapper));
        return returnPage;
    }
}
